package org.example.service.market;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.dao.market.CImageDao;
import org.example.dao.dao.market.CStoreDao;
import org.example.dao.dto.market.StoreDto;
import org.example.dao.entity.market.CImage;
import org.example.dao.entity.market.CStore;
import org.example.exception.AuthException;
import org.example.schedul.MyScheduledTask;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.oss.minio.MinioConstant;
import org.example.skytool.oss.minio.MinioUtil;
import org.example.utils.market.Store.RedisStoreService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class StoreService {

    @Resource
    private CStoreDao cStoreDao;

    @Resource
    private CImageDao cImageDao;

    @Resource
    private IdWorker idWorker;

    @Resource
    private ContextUtil contextUtil;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private RedisStoreService redisStoreService;

    /**
     * 为商户创建店铺（管理）
     */
    public CStore addStore(CStore store) {
        store.setId(idWorker.nextId());
        cStoreDao.insert(store);
        redisStoreService.setCacheStoreIds(store.getBusinessId());
        redisStoreService.createStoreIds();
        return store;
    }

    /**
     * 创建店铺(商户)
     */
    public CStore createStore(CStore store) {
        Long id = contextUtil.getId();
        store.setId(idWorker.nextId());
        store.setBusinessId(id);
        cStoreDao.insert(store);
        redisStoreService.setCacheStoreIds(id);
        redisStoreService.createStoreIds();
        return store;
    }

    /**
     * 查询本人店铺(商户)
     */
    public IPage<CStore> selectStoreByMine(Long limit, Long start) {
        Long id = contextUtil.getId();
        return redisStoreService.getCacheStoresForBusiness(id, limit.intValue(), start.intValue());
    }

    /**
     * 查询店铺(管理)
     */
    public IPage<CStore> selectStore(Long limit, Long start) {
        Page<CStore> page = cStoreDao.selectPage(new Page<>(start, limit), new LambdaQueryWrapper<>());
        return page;
    }

    /**
     * 查询店铺(用户)
     */
    public IPage<CStore> selectStoreForUser(Long limit, Long start) {
        return redisStoreService.getCacheStoresForUsers(limit.intValue(), start.intValue());
    }

    /**
     * 修改店铺信息(商户)
     */
    public CStore updateStore(CStore store) {
        store.setUpdateTime(new Date());
        cStoreDao.updateById(store);
        Long id = contextUtil.getId();
        redisStoreService.setCacheStoreIds(id);
        redisStoreService.createStoreIds();
        return store;
    }

    /**
     * 修改店铺主页图像(商户)
     */
    public CStore updateStoreIcon(Long storeId, MultipartFile file) {
        CStore store = cStoreDao.selectById(storeId);
        String url = minioUtil.upload(file, MinioConstant.STORE, storeId.toString());
        store.setImage(url);
        store.setUpdateTime(new Date());
        cStoreDao.updateById(store);
        redisStoreService.setCacheStoreIds(store.getBusinessId());
        redisStoreService.createStoreIds();
        return store;
    }

    /**
     * 上传店铺图像(商户)
     */
    public List<CImage> uploadStoreImages(MultipartFile[] files, Long storeId) {
        List<CImage> cImageList = new ArrayList<>();
        int temp = 1;
        for (MultipartFile file : files) {
            String url = minioUtil.upload(file, MinioConstant.STORE, storeId.toString() + temp);
            CImage cImage = new CImage();
            cImage.setId(idWorker.nextId());
            cImage.setUrl(url);
            cImage.setLinkId(storeId);
            cImageList.add(cImage);
            cImageDao.insert(cImage);
            temp++;
        }
        return cImageList;
    }

    /**
     * 查看店铺图像(商户)
     */
    public List<CImage> selectStoreImages(Long storeId) {
        List<CImage> cImageList = cImageDao.selectImages(storeId, null);
        return cImageList;
    }

    /**
     * 删除店铺(管理)
     */
    public void deleteStore(CStore store) {
        store.setUpdateTime(new Date());
        store.setIsValid(0);
        cStoreDao.updateById(store);
        CStore cStore = cStoreDao.selectById(store.getId());
        redisStoreService.setCacheStoreIds(cStore.getBusinessId());
        redisStoreService.createStoreIds();
    }

    /**
     * 根据店铺ids获取店铺Dto
     */
    public List<StoreDto> getScoreDtoByIds(List ids){
        LambdaQueryWrapper<CStore> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CStore::getIsValid,1);
        lambdaQueryWrapper.in(CStore::getId,ids);
        List<CStore> list = cStoreDao.selectList(lambdaQueryWrapper);
        List<StoreDto> list1 = new ArrayList<>();
        for(CStore store : list){
            StoreDto storeDto = new StoreDto(store);
            list1.add(storeDto);
        }
        return list1;
    }

    public void checkStore(Long storeId) {
        CStore store = cStoreDao.selectById(storeId);
        Long businessId = contextUtil.getId();
        if (!Objects.equals(store.getBusinessId(), businessId)) {
            throw new AuthException("该店铺不在商户名下");
        }
    }


}
